﻿
@{
    ViewBag.Title = "机关科室测评";
    Layout = "~/Views/Shared/_EvalLayout.cshtml";
}

@model List<WebApp.ViewModels.Evaluate.OfficeDetpResultModel>

<input type="hidden" id="evalId" value="@ViewBag.EvalId" />

<div class="jjdy_box w1320">
    <div class="jjdy_l left" style="width:100%;">
        <div class="bbs_list border">
            <div class="bbs_menu">当前位置：<a href="~/Eval/Index">大机段管理人员年度测评系统</a> &gt; @ViewBag.Title</div>
            <div class="bbscount_tit">
                <h1>@ViewBag.Title</h1>
            </div>
            <div class="bbs_count1">
                <div class="bbs_count_r" style="width:100%;">
                    <div class="bbs_count_neirong">
                        <table class="table table-bordered table-hover">
                            <thead>
                                <tr>
                                    <th rowspan="2">科室名称</th>
                                    <th class="split" colspan="3">服务态度</th>
                                    <th class="split" colspan="3">办事效率</th>
                                    <th class="split" colspan="3">深入现场</th>
                                    <th class="split" colspan="3">首问负责</th>
                                    <th class="split" colspan="3">廉洁自律</th>
                                    <th class="split" colspan="3">综合评价</th>
                                    @*<th rowspan="2" style="width:6%;"></th>*@
                                </tr>
                                <tr>
                                    <th class="split" style="width:4%;">@(ViewBag.ResultName_1)</th>
                                    <th style="width:4%;">@(ViewBag.ResultName_2)</th>
                                    <th style="width:4%;">@(ViewBag.ResultName_3)</th>
                                    <th class="split" style="width:4%;">@(ViewBag.ResultName_1)</th>
                                    <th style="width:4%;">@(ViewBag.ResultName_2)</th>
                                    <th style="width:4%;">@(ViewBag.ResultName_3)</th>
                                    <th class="split" style="width:4%;">@(ViewBag.ResultName_1)</th>
                                    <th style="width:4%;">@(ViewBag.ResultName_2)</th>
                                    <th style="width:4%;">@(ViewBag.ResultName_3)</th>
                                    <th class="split" style="width:4%;">@(ViewBag.ResultName_1)</th>
                                    <th style="width:4%;">@(ViewBag.ResultName_2)</th>
                                    <th style="width:4%;">@(ViewBag.ResultName_3)</th>
                                    <th class="split" style="width:4%;">@(ViewBag.ResultName_1)</th>
                                    <th style="width:4%;">@(ViewBag.ResultName_2)</th>
                                    <th style="width:4%;">@(ViewBag.ResultName_3)</th>
                                    <th class="split" style="width:4%;">@(ViewBag.ResultName_1)</th>
                                    <th style="width:4%;">@(ViewBag.ResultName_2)</th>
                                    <th style="width:4%;">@(ViewBag.ResultName_3)</th>
                                </tr>
                            </thead>
                            <tbody>
                                @foreach (var item in Model)
                                {
                                    <tr data-deptid="@(item.Dept_id)">
                                        <td>
                                            @(item.Deptname)
                                            <input type="hidden" id="result_1_@(item.Dept_id)" value="@item.Result_1" class="datafield" data-fieldname="Result_1" />
                                            <input type="hidden" id="result_2_@(item.Dept_id)" value="@item.Result_2" class="datafield" data-fieldname="Result_2" />
                                            <input type="hidden" id="result_3_@(item.Dept_id)" value="@item.Result_3" class="datafield" data-fieldname="Result_3" />
                                            <input type="hidden" id="result_4_@(item.Dept_id)" value="@item.Result_4" class="datafield" data-fieldname="Result_4" />
                                            <input type="hidden" id="result_5_@(item.Dept_id)" value="@item.Result_5" class="datafield" data-fieldname="Result_5" />
                                            <input type="hidden" id="result_6_@(item.Dept_id)" value="@item.Result_6" class="datafield" data-fieldname="Result_6" />
                                        </td>
                                        <td class="text-center col-alt split"><a class="eval-checkbox" data-fieldname="Result_1" data-value="10"></a></td>
                                        <td class="text-center col-alt"><a class="eval-checkbox" data-fieldname="Result_1" data-value="20"></a></td>
                                        <td class="text-center col-alt"><a class="eval-checkbox" data-fieldname="Result_1" data-value="30"></a></td>
                                        <td class="text-center split"><a class="eval-checkbox" data-fieldname="Result_2" data-value="10"></a></td>
                                        <td class="text-center"><a class="eval-checkbox" data-fieldname="Result_2" data-value="20"></a></td>
                                        <td class="text-center"><a class="eval-checkbox" data-fieldname="Result_2" data-value="30"></a></td>
                                        <td class="text-center col-alt split"><a class="eval-checkbox" data-fieldname="Result_3" data-value="10"></a></td>
                                        <td class="text-center col-alt"><a class="eval-checkbox" data-fieldname="Result_3" data-value="20"></a></td>
                                        <td class="text-center col-alt"><a class="eval-checkbox" data-fieldname="Result_3" data-value="30"></a></td>
                                        <td class="text-center split"><a class="eval-checkbox" data-fieldname="Result_4" data-value="10"></a></td>
                                        <td class="text-center"><a class="eval-checkbox" data-fieldname="Result_4" data-value="20"></a></td>
                                        <td class="text-center"><a class="eval-checkbox" data-fieldname="Result_4" data-value="30"></a></td>
                                        <td class="text-center col-alt split"><a class="eval-checkbox" data-fieldname="Result_5" data-value="10"></a></td>
                                        <td class="text-center col-alt"><a class="eval-checkbox" data-fieldname="Result_5" data-value="20"></a></td>
                                        <td class="text-center col-alt"><a class="eval-checkbox" data-fieldname="Result_5" data-value="30"></a></td>
                                        <td class="text-center col-alt split"><a class="eval-checkbox" data-fieldname="Result_6" data-value="10"></a></td>
                                        <td class="text-center col-alt"><a class="eval-checkbox" data-fieldname="Result_6" data-value="20"></a></td>
                                        <td class="text-center col-alt"><a class="eval-checkbox" data-fieldname="Result_6" data-value="30"></a></td>
                                        @*<td class="text-center col-alt"><a class="btn btn-xs check-all">全部满意</a></td>*@
                                    </tr>
                                }
                            </tbody>
                        </table>
                    </div>
                </div>
                <div class="bbs_count_r" style="margin:0 0 20px 20px;">
                    @if (ViewBag.CanEdit)
                    {
                        <button id="btnsubmit" class="btn btn-lg btn-primary">提交</button>
                    }
                    else
                    {
                        <button id="btnsubmit" disabled="disabled" class="btn btn-lg btn-default">提交</button>
                    }
                </div>
            </div>
        </div>
    </div>

</div>

@section scripts{
    <script type="text/javascript">
        var canEdit = @(ViewBag.CanEdit ? "true": "false");

        $(document).ready(function () {
            $(".datafield").each(function () {
                var $row = $(this).closest("tr");
                var fieldname = $(this).attr("data-fieldname");
                var value = $(this).val();

                $row.find("[data-fieldname='" + fieldname + "'][data-value='" + value + "']").addClass("checked");
            });

            //$(".check-all").click(function(){
            //    var $row = $(this).closest("tr");
            //    $row.find("[data-value=10]").click();
            //})

            $(".eval-checkbox").click(function (e) {
                if(!canEdit) return;

                var $row = $(this).closest("tr");
                var fieldname = $(this).attr("data-fieldname");
                var deptId = $row.attr("data-deptid");
                //$row.removeClass("error");
                $row.find(".eval-checkbox[data-fieldname=" + fieldname + "]").removeClass("checked");
                $(this).addClass("checked");

                var value = $(this).attr("data-value");
                $row.find("input[data-fieldname=" + fieldname + "]").val(value);

                //保存结果
                saveResult(deptId, fieldname, value);
            });

            $("#btnsubmit").click(function (e) {
                e.preventDefault();
                
                if(!canEdit) return;

                var isvalid = true;
                $(".datafield").each(function () {
                    if ($(this).val() == "") {
                        //$(this).closest("tr").addClass("error");
                        isvalid = false;
                    }
                });
                if (!isvalid) {
                    alert("所有科室测评结果必须都填写完整！");
                    return;
                }

                if (confirm("结果提交后不能再修改，确认要提交测评结果吗？")) {
                    submitResult();
                }
            });
            
            if(!canEdit){
                setTimeout(function(){
                    alert("您的测评结果已提交，不能修改！");
                },100);
            }
        });

        function saveResult(deptId, fieldname, result) {
            $.ajax({
                url: baseurl + 'Eval/SaveOfficeItemResult',
                method: 'post',
                data: {
                    evalId: $("#evalId").val(),
                    deptId: deptId,
                    fieldname: fieldname,
                    result: result
                },
                success: function (result) {
                    //console.log(result);
                    if(result.Ret == 1){
                        alert(e.Msg);
                    }
                    else if(result.Ret == 2){
                        alert("登录已过期，请重新登录！");
                        location.href = baseurl + "Eval/Login";
                    }
                }
            });
        }

        function submitResult() {
            $.ajax({
                url: baseurl + 'Eval/SubmitOfficeItemResult',
                method: 'post',
                data: {
                    evalId: $("#evalId").val()
                },
                success: function (result) {
                    if(result.Ret == 1){
                        alert(e.Msg);
                    }
                    else if(result.Ret == 2){
                        alert("登录已过期，请重新登录！");
                        location.href = baseurl + "Eval/Login";
                    }else{
                        alert("提交成功！");
                        location.href = baseurl + "Eval/Index";
                    }
                }
            });
        }
    </script>
}